package com.alibaba.citrus.ots.b2b.trade.order.facade.service;

import com.alibaba.citrus.ots.b2b.trade.order.repository.OrderQueryRepository;
import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsb2btradecenter.api.orderquery.service.OrderQueryService;
import com.epoch.app.otsb2btradecenter.dto.request.OrderPageQueryRequest;
import com.epoch.app.otsb2btradecenter.dto.request.OrderQueryRequest;
import com.epoch.app.otsb2btradecenter.dto.response.OrderPageQueryResponse;
import com.epoch.app.otsb2btradecenter.dto.response.OrderQueryResponse;
import com.epoch.app.otsb2btradecenter.model.dto.PageQueryOrderEsListRequest;
import com.epoch.app.otspricecenter.api.creditquery.service.CreditQueryService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("OrderQueryService")
@Primary
public class OrderQueryServiceImpl implements OrderQueryService {
    @Resource
    private CreditQueryService creditQueryService;
    @Resource
    private OrderQueryRepository orderQueryRepository;
    private Log log = Log.getLogger(OrderQueryServiceImpl.class);
    @Override
    @FacadeInvoker(errorCode = "OTS-05-001-10-16-001")
    public Result<List<OrderPageQueryResponse>> pageQuery(OrderPageQueryRequest request) {
        PageResult<List<OrderPageQueryResponse>> listPageResult = orderQueryRepository.pageQuery(request);
        return Result.listSuccess(listPageResult.getContent(),listPageResult.getTotal());
    }

    @Override
    @FacadeInvoker("OTS-05-001-10-16-002")
    public Result<OrderQueryResponse> query(OrderQueryRequest request) {
        OrderQueryResponse response = orderQueryRepository.query(request);
//        response.setCustomerAccounts("-");
        //实时查询信贷
//        com.epoch.app.otspricecenter.model.dto.QueryCreditRequest creditRequest = new com.epoch.app.otspricecenter.model.dto.QueryCreditRequest();
//        creditRequest.setChannelCode(response.getBuzChannel());
//        creditRequest.setOrganizationCode(response.getSalesOrganization());
//        creditRequest.setItemGroupCode(response.getGoodsGroup());
//        creditRequest.setCustomerCode(response.getCustomerCode());
//        creditRequest.setSaleOrderId(response.getId());
//        Result<CreditReponse> creditReponseResult = creditQueryService.queryCreditBalance(creditRequest);
//        if (creditReponseResult.isSuccess()) {
//            String balance = creditReponseResult.getResult().getBalance();
//            response.setCustomerAccounts(balance);
//        } else {
//            log.error("查询信贷余额失败：" + response.getOrderBizId());
//        }
        return Result.success(response);
    }


    @Override
    @FacadeInvoker(errorCode = "OTS-05-001-10-16-001")
    public Result<List<OrderPageQueryResponse>> pageQueryOrderListES(PageQueryOrderEsListRequest request){
        Result<List<OrderPageQueryResponse>> listResult = orderQueryRepository.pageQueryES(request);
        return Result.listSuccess(listResult.getResult(),listResult.getTotal());
    }

}
